package com.zr.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * 过滤网页
 * @author zengpeng
 *
 */
public class LoginFilter implements Filter{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		 HttpServletRequest request=(HttpServletRequest)arg0;
		  HttpServletResponse response=(HttpServletResponse)arg1;
		  HttpSession session=request.getSession();
		  
		  //usermodel,需要通过login.jsp 登陆后向session中写入用户信息，标示用户已经登陆
		  if( null==session.getAttribute("usermodel") ||"".equals(session.getAttribute("usermodel"))){
		   String errors = "请先登陆，再访问此页面!";
		   request.setAttribute("errors", errors);
		   response.sendRedirect("login.jsp");
		  }else{
		   arg2.doFilter(request, response);
		  }
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
